package Utils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.Scanner;

public class TSUtility {
    private static Scanner scanner = new Scanner(System.in);

    public static void writeFile(String s) throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("E:\\JAVAWork\\item01\\src\\FileTest\\login.txt",true));

        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String data = sdf.format(d);

        String log=data+s;
        bw.write(log);
        bw.newLine();
        bw.close();
    }

    public static char readMenuSelection_2() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2') {
                System.out.print("选择错误，请重新输入：");
            } else {
                break;
            }
        }
        return c;
    }

    public static char readMenuSelection_3() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2'&& c != '3') {
                System.out.print("选择错误，请重新输入：");
            } else {
                break;
            }
        }
        return c;
    }

    public static char readMenuSelection_4() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4') {
                System.out.print("选择错误，请重新输入：");
            } else {
                break;
            }
        }
        return c;
    }

    public static char readMenuSelectionPro() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5') {
                System.out.print("选择错误，请重新输入：");
            } else{
                break;
            }
        }
        return c;
    }

    public static char readMenuSelectionPro_6() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' &&
                    c != '3' && c != '4' && c != '5'&& c != '6') {
                System.out.print("选择错误，请重新输入：");
            } else{
                break;
            }
        }
        return c;
    }


    public static void readReturn() {
        System.out.print("按回车键继续...");
        readKeyBoard(100, true);
    }

    public static int readInt() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(6, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static int readInt_year() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(4, false);
            try {
                n = Integer.parseInt(str);
                if (n<1990||n>2022){
                    throw new Exception("请输入年份1990-2022");
                }
                break;
            } catch (Exception e) {
                System.out.print(e.getMessage());
            }
        }
        return n;
    }

    public static int readInt_mouth() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                if (n>12||n<1){
                    throw new Exception("请输入1-12");
                }
                break;
            } catch (Exception e) {
                System.out.print(e.getMessage());
            }
        }
        return n;
    }

    public static int readInt_day() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                if (n>30||n<1){
                    throw new Exception("请输入1-31");
                }
                break;
            } catch (Exception e) {
                System.out.print(e.getMessage());
            }
        }
        return n;
    }

    public static int readInt_8() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(8, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static int readInt_11() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(11, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static String readPhone_11() {
        String str;
        for (; ; ) {
            str = readKeyBoard(11, false);
            try {
                if (str.length()!=11) {
                    System.out.print("输入长度必须为11位，请重新输入：");
                    continue;
                }
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return str;
    }

    public static int readstock() {
        int n;
        for (; ; ) {
            String str = readKeyBoard(6, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static Double readDouble() {
        Double n;
        for (; ; ) {
            String str = readKeyBoard(6, false);
            try {
                n = Double.parseDouble(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static char readConfirmSelection() {
        char c;
        for (; ; ) {
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.print("选择错误，请重新输入：");
            }
        }
        return c;
    }
    /**
     从键盘读取一个长度不超过limit的字符串，并将其作为方法的返回值。
     如果用户不输入字符而直接回车，方法将以defaultValue 作为返回值。
     */
    public static String readString(int limit, String defaultValue) {
        String str = readKeyBoard(limit, true);
        return str.equals("") ? defaultValue : str;
    }

    public static String readKeyBoard(int limit, boolean blankReturn) {
        String line = "";

        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.length() == 0) {
                if (blankReturn) {
                    return line;
                } else {
                    continue;
                }
            }

            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度（不大于" + limit + "）错误，请重新输入：");
                continue;
            }
            break;
        }

        return line;
    }

    public static String sex(int limit, boolean blankReturn) {
        String line = "";

        while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            if (line.length() == 0) {
                if (blankReturn) {
                    return line;
                } else {
                    continue;
                }
            }

            if (line.length() < 1 || line.length() > limit) {
                System.out.print("输入长度（不大于" + limit + "）错误，请重新输入：");
                continue;
            }

            if (line.equals("男")||line.equals("女")) {
                return line;
            }
            else {
                System.out.print("输入错误，请重新输入：");
                continue;
            }
        }

        return line;
    }

    public static void loadSpecialEffects() throws InterruptedException {
        System.out.println("请稍等：");
        for (int i1 = 1; i1 <= 100; i1++) {
            System.out.print("加载中：" + i1 + "%");
            Thread.sleep(new Random().nextInt(25) + 1);
            if (i1 == 100) {
                Thread.sleep(50);
            }
            System.out.print("\r");
        }
    }
}